package com.tanhua.app.service;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.config.template.AipTemplate;
import com.tanhua.config.template.OssTemplate;
import com.tanhua.config.template.SmsTemplate;
import com.tanhua.dubbo.api.AttentionApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.pojo.AttentionList;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class UserInfoService {
    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private UserApi userApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipTemplate aipTemplate;

    @DubboReference
    private UserLikeApi userLikeApi;

    @DubboReference
    private AttentionApi attentionApi;

    private String CHECKCODE = "UPDATEPHONE";

    //查找用户信息
    public UserInfoVo findById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        if (userInfo.getAge() !=null){
            userInfoVo.setAge(userInfo.getAge().toString());
        }
        return userInfoVo;
    }

    //修改用户信息
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    //发送验证码
    public void sendCode(String mobile) {
        String code = "123456";
        //smsTemplate.sendSms(mobile,code);
        //将验证码存入到redis中
        redisTemplate.opsForValue().set(CHECKCODE+mobile,code, Duration.ofMinutes(5));
    }

    //校验用户修改手机号验证码
    public Boolean checkVerificationCode(String verificationCode) {
        //1.从Redis中获取验证码
        String redisCodes = redisTemplate.opsForValue().get(CHECKCODE + UserHolder.getMobile());
        if (!verificationCode.equals(redisCodes) || StringUtils.isBlank(redisCodes)){
            throw new RuntimeException("验证码错误");
        }else {
            return true;
        }
    }

    //保存用户手机号
    public void savePhone(String phone) {
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //2.调用api保存手机号
        userApi.updatePhoneById(userId,phone);
        //3.修改token
        Map params = new HashMap();
        params.put("id",userId);
        params.put("mobile",phone);
    }

    //更新头像
    public void updateAvatar(MultipartFile headPhoto) throws IOException {
        //1.将照片上传到阿里云oss
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2.调用百度云判断是否包含人脸
        boolean detect = aipTemplate.detect(imageUrl);
        //3.不包含人脸抛出异常
        if (!detect){
            throw new RuntimeException("不包含人脸");
        }else {
            //4.包含人脸调用api更新
            UserInfo userInfo = new UserInfo();
            userInfo.setId(UserHolder.getUserId());
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    //互相喜欢，喜欢，粉丝 - 统计
    public Map counts() {
        Long userId = UserHolder.getUserId();
        Map map = new HashMap();
        //查询所有喜欢我的
        List<UserLike> userLikes = userLikeApi.selectLove(userId);
        List<Long> ids = userLikes.stream().map(UserLike::getUserId).collect(Collectors.toList());
        map.put("loveCount",ids.size());
        //查询互相喜欢
        List<UserLike> eachLove = userLikeApi.selectEachLove(userId,ids);
        map.put("eachLoveCount",eachLove.size());
        //查询粉丝
        List<AttentionList> count = attentionApi.findCounts(userId);
        map.put("fanCount",count.size());

        return map;
    }
}
